package com.liu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.dao.EmployeeDao;
import com.liu.dto.EmpTjDto;
import com.liu.entity.Employee;
import com.liu.service.intf.EmployeeService;
import com.liu.vo.PageBeanJson;
import com.liu.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeDao dao;
    @Override
    public R save(Employee employee) {
        //执行新增操作
        if(dao.add(employee)>0){
            //成功
            return R.ok();
        }else{
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBeanJson queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果
        PageInfo<Employee> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回分页的数据
        return PageBeanJson.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(int id) {
        //1.验证参数
        if(id>0){
            //2.操作数据库 验证是否成功
            if(dao.del(id)>0){
                //删除成功
                return R.ok();
            }else{
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }

    @Override
    public R update(Employee employee) {
        if(dao.update(employee)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public PageBeanJson queryPageWhere(String empname, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(empname)){
            empname=null;
        }else{
            empname="%"+empname+"%";//包含  模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果
        PageInfo<Employee> pageInfo=new PageInfo<>(dao.selectWhere(empname));
        //4.返回分页的数据
        return PageBeanJson.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
    @Override
    public List<Employee> queryAll() {
        return dao.selectAll();
    }
    @Override
    public R saveBatch(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            //excel解析
            List<Employee> list= EasyExcel.read(file.getInputStream(), Employee.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {}
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {}
            }).sheet().doReadSync();
            //执行dao 批量新增
            if(dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }
    @Override
    public R queryTj() {
        //1.查询统计数据
        List<EmpTjDto> list=dao.selectTj();
        //2.根据前端 组装数据
        Map<String,List<Object>> map=new HashMap<>();
        map.put("empname",new ArrayList<>());//员工姓名
        map.put("salary",new ArrayList<>());//薪资
        //3.组装数据  函数式编程
        list.forEach(f->{
            map.get("empname").add(f.getEmpname());
            map.get("salary").add(f.getSalary());
        });
        return R.ok(map);
    }
}

